package com.atts.baccarat.table.impl;

import com.atts.baccarat.table.TableItem;
import com.atts.baccarat.table.TableItemColor;
import com.atts.baccarat.table.TableItemShape;

/**
 * 计算得出的预想表格
 * 
 * @author Administrator
 * 
 */
public class NextRoad extends VerticalSeqTable {

	private final BigRoad bigRoad;

	public NextRoad(final BigRoad bigRoad) {
		// 3行
		super(3);
		this.bigRoad = bigRoad;
	}

	@Override
	public boolean isValidItem(final TableItem item) {

		return (item.getShape() == TableItemShape.BIG_EYE_ROAD
				|| item.getShape() == TableItemShape.SMALL_ROAD || item
				.getShape() == TableItemShape.COCKROACH_ROAD)
				&& (item.getColor() != TableItemColor.GREEN);
	}

	/**
	 * 根据大路数据，计算预期数据
	 * 
	 * @param bigRoad
	 *            bigRoad
	 */
	public void calculate() {

		// 清空。准备下一次计算
		super.clear();

		// 没有数据，不做计算
		if (this.bigRoad.getCurrentColumnIndex() == 0
				&& this.bigRoad.getCurrentRowIndex() == 0) {
			return;
		}

		// 得到大路当前列的第一个元素。判定是庄赢/闲赢
		final TableItem first = this.bigRoad.getItem(0,
				this.bigRoad.getCurrentColumnIndex());

		// 默认庄赢
		int columnIndex = 0;
		if (first.getColor() == TableItemColor.BLUE) {
			// 闲赢
			columnIndex = 1;
		}

		final TableItem bigEye = this.calBigEye();
		final TableItem small = this.calSmall();
		final TableItem cockroach = this.calCockroach();

		if (columnIndex == 0) {
			// 庄赢
			this.add(bigEye);
			this.add(small);
			this.add(cockroach);
			this.add(this.xorColor(bigEye));
			this.add(this.xorColor(small));
			this.add(this.xorColor(cockroach));

		} else {
			// 闲赢
			this.add(this.xorColor(bigEye));
			this.add(this.xorColor(small));
			this.add(this.xorColor(cockroach));
			this.add(bigEye);
			this.add(small);
			this.add(cockroach);
		}
	}

	/**
	 * 颜色取反
	 * 
	 * @param item
	 *            item
	 * @return TableItem
	 */
	private TableItem xorColor(final TableItem item) {

		TableItem result = null;
		if (item != null) {
			if (item.getColor() == TableItemColor.BLUE) {
				result = new TableItem(TableItemColor.RED, item.getShape());
			} else if (item.getColor() == TableItemColor.RED) {
				result = new TableItem(TableItemColor.BLUE, item.getShape());
			} else {
				throw new IllegalArgumentException();
			}
		}

		return result;
	}

	/**
	 * 计算大眼仔
	 * 
	 * @return TableItem
	 */
	private TableItem calBigEye() {

		if (this.bigRoad.getCurrentColumnIndex() < 1) {
			return null;
		}

		final TableItem left1 = this.getLeft1();
		final TableItem leftTop1 = this.getLeftTop1();

		TableItem result = null;
		if (left1 != null) {
			result = new TableItem(TableItemColor.RED,
					TableItemShape.BIG_EYE_ROAD);
		} else {

			if (leftTop1 != null) {
				result = new TableItem(TableItemColor.BLUE,
						TableItemShape.BIG_EYE_ROAD);
			} else {
				result = new TableItem(TableItemColor.RED,
						TableItemShape.BIG_EYE_ROAD);
			}
		}

		return result;
	}

	/**
	 * 计算小路
	 * 
	 * @return TableItem
	 */
	private TableItem calSmall() {

		if (this.bigRoad.getCurrentColumnIndex() < 2) {
			return null;
		}

		final TableItem left1 = this.getLeft2();
		final TableItem leftTop1 = this.getLeftTop2();

		TableItem result = null;
		if (left1 != null) {
			result = new TableItem(TableItemColor.RED,
					TableItemShape.SMALL_ROAD);
		} else {

			if (leftTop1 != null) {
				result = new TableItem(TableItemColor.BLUE,
						TableItemShape.SMALL_ROAD);
			} else {
				result = new TableItem(TableItemColor.RED,
						TableItemShape.SMALL_ROAD);
			}
		}

		return result;
	}

	/**
	 * 计算甲由路
	 * 
	 * @return TableItem
	 */
	private TableItem calCockroach() {

		if (this.bigRoad.getCurrentColumnIndex() < 3) {
			return null;
		}

		final TableItem left1 = this.getLeft3();
		final TableItem leftTop1 = this.getLeftTop3();

		TableItem result = null;
		if (left1 != null) {
			result = new TableItem(TableItemColor.RED,
					TableItemShape.COCKROACH_ROAD);
		} else {

			if (leftTop1 != null) {
				result = new TableItem(TableItemColor.BLUE,
						TableItemShape.COCKROACH_ROAD);
			} else {
				result = new TableItem(TableItemColor.RED,
						TableItemShape.COCKROACH_ROAD);
			}
		}

		return result;
	}

	/**
	 * 得到当前单元格的左边1个单元格内元素
	 * 
	 * @param rowIndex
	 *            rowIndex
	 * @param columnIndex
	 *            columnIndex
	 * @return TableItem
	 */
	private TableItem getLeft1() {

		final int c = this.bigRoad.getCurrentColumnIndex() - 1;
		return this.bigRoad.getItem(this.bigRoad.getCurrentRowIndex(), c);

	}

	/**
	 * 得到当前单元格的左上１个单元格内元素
	 * 
	 * @param rowIndex
	 *            rowIndex
	 * @param columnIndex
	 *            columnIndex
	 * @return TableItem
	 */
	private TableItem getLeftTop1() {

		final int c = this.bigRoad.getCurrentColumnIndex() - 1;
		final int r = this.bigRoad.getCurrentRowIndex() - 1;
		return this.bigRoad.getItem(r, c);

	}

	/**
	 * 得到当前单元格的左边2个单元格内元素
	 * 
	 * @param rowIndex
	 *            rowIndex
	 * @param columnIndex
	 *            columnIndex
	 * @return TableItem
	 */
	private TableItem getLeft2() {

		final int c = this.bigRoad.getCurrentColumnIndex() - 2;
		return this.bigRoad.getItem(this.bigRoad.getCurrentRowIndex(), c);

	}

	/**
	 * 得到当前单元格的左上２个单元格内元素
	 * 
	 * @param rowIndex
	 *            rowIndex
	 * @param columnIndex
	 *            columnIndex
	 * @return TableItem
	 */
	private TableItem getLeftTop2() {

		final int c = this.bigRoad.getCurrentColumnIndex() - 2;
		final int r = this.bigRoad.getCurrentRowIndex() - 1;
		return this.bigRoad.getItem(r, c);

	}

	/**
	 * 得到当前单元格的左边3个单元格内元素
	 * 
	 * @param rowIndex
	 *            rowIndex
	 * @param columnIndex
	 *            columnIndex
	 * @return TableItem
	 */
	private TableItem getLeft3() {

		final int c = this.bigRoad.getCurrentColumnIndex() - 3;
		return this.bigRoad.getItem(this.bigRoad.getCurrentRowIndex(), c);

	}

	/**
	 * 得到当前单元格的左上３个单元格内元素
	 * 
	 * @param rowIndex
	 *            rowIndex
	 * @param columnIndex
	 *            columnIndex
	 * @return TableItem
	 */
	private TableItem getLeftTop3() {

		final int c = this.bigRoad.getCurrentColumnIndex() - 3;
		final int r = this.bigRoad.getCurrentRowIndex() - 1;
		return this.bigRoad.getItem(r, c);

	}
}
